package com.faithbj.process.impl;

import com.faithbj.process.Condition;
import com.faithbj.process.State;
import com.faithbj.process.TaskContentFilter;
import com.faithbj.process.TaskContentProvider;
import com.google.common.base.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.Map;

/**
 * User: xiongyuanwei
 * Date: 12-7-15
 * Time: 上午12:21
 */
@Component
public class ContentSearcherFactory {

    @Autowired
    private TaskContentProvider taskContentProvider;

    @Autowired
    @Qualifier("processEmf")
    private EntityManagerFactory emf;

    private EntityManager em;

    @PostConstruct
    private void init() {
        em = emf.createEntityManager();
    }

    public <T> ContentSearcher<T> createContentSearcher(Condition<T> condition, String userId, State state,
            int first, int limit, Function<Map<String, Object>, T> extractFn, TaskContentFilter filter) {
        return new ContentSearcher<T>(condition, userId, state.getName(), first, limit, taskContentProvider, em, extractFn, filter);
    }
}
